package com.dreamder.view;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dreamder.model.CategoriaBeanTO;
import com.dreamder.model.SonhoBeanTO;
import com.dreamder.service.SonhoService;
import com.google.gson.Gson;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class BuscarSonhoActivity extends Activity {

	private ListView listaSonhos;
	ListAdapter listAdapter;
	Intent i;
	List<SonhoBeanTO> sonhos;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_buscar_sonho);

		listaSonhos = (ListView) findViewById(R.id.buscarSonhosListView);

		listaSonhos.setAdapter(listAdapter);

		listaSonhos.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				SonhoBeanTO sonho = (SonhoBeanTO) parent
						.getItemAtPosition(position);

				i = new Intent(BuscarSonhoActivity.this,
						DetalheSonhoActivity.class);
				i.putExtra("sonho", sonho);
				startActivity(i);

			}
		});

	}

	public void buscar(View v) {

		TextView termo = (TextView) findViewById(R.id.txtBuscaSonho);
		String palavra = termo.getText().toString();

		BuscarSonhoTask task = new BuscarSonhoTask();

		task.execute(palavra);

	}

	public class BuscarSonhoTask extends AsyncTask<String, Void, String> {

		private ProgressDialog progress;
		private SonhoService service;

		@Override
		protected void onPreExecute() {
			progress = ProgressDialog.show(BuscarSonhoActivity.this, "Aguarde",
					"Procurando sonhos...", true, true);
		}

		@Override
		protected String doInBackground(String... params) {
			service = new SonhoService();
			return service.pesquisarSonho(params[0]);
		}

		@Override
		protected void onPostExecute(String result) {

			progress.dismiss();

			if (!"ERRO".equals(result)) {
				try {

					JSONObject json = new JSONObject(result);

					JSONArray arrayJson = json.getJSONArray("sonhos");

					sonhos = new ArrayList<SonhoBeanTO>();

					SonhoBeanTO sonho;
					CategoriaBeanTO cat;

					if (arrayJson.length() < 1) {
						Toast.makeText(BuscarSonhoActivity.this,
								"Nenhum sonho encontrado!", Toast.LENGTH_LONG)
								.show();
					}

					for (int i = 0; i < arrayJson.length(); i++) {

						JSONObject item = (JSONObject) arrayJson.get(i);
						sonho = new SonhoBeanTO();
						cat = new CategoriaBeanTO();

						// TODO set propriedades do SONHO

						// exemplo:
						sonho.setResumo(item.getString("resumo"));
						sonho.setTitulo(item.getString("titulo"));
						sonho.setDsDetalhada(item.getString("dsDetalhada"));
						sonho.setId(item.getLong("id"));

						String catStr = item.getString("categoria");
						Gson gson = new Gson();
						cat = gson.fromJson(catStr, CategoriaBeanTO.class);

						sonho.setCategoria(cat);
						sonhos.add(sonho);
					}

					listAdapter = new ArrayAdapter<SonhoBeanTO>(
							BuscarSonhoActivity.this,
							android.R.layout.simple_list_item_1, sonhos);

					listaSonhos.setAdapter(listAdapter);

				} catch (JSONException e) {
					e.printStackTrace();
					Toast.makeText(BuscarSonhoActivity.this, "ERRO",
							Toast.LENGTH_LONG).show();
				}
			} else {
				Toast.makeText(BuscarSonhoActivity.this, "ERRO",
						Toast.LENGTH_LONG).show();
			}

		}

	}
}
